package com.lb.jwitter.action;


import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import java.util.Map; 
import org.springframework.test.context.ContextConfiguration; 
import com.lb.jwitter.service.TblQuyenChiTietService; 
import com.lb.jwitter.service.TblTaiKhoan; 
import com.opensymphony.xwork2.ActionContext; 
import java.util.Date; 
import com.lb.jwitter.util.ApplicationContextProvider; 
import java.util.GregorianCalendar; 
import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblDKMHDotDKHocVien;
import com.lb.jwitter.service.TblDKMHDotDKHocVienService;
import com.lb.jwitter.service.TblDKMHDotDK;
import com.lb.jwitter.service.TblDKMHDotDKService;

import com.lb.jwitter.service.TblThongTinHocVien;
import com.lb.jwitter.service.TblThongTinHocVienService;

import com.lb.jwitter.service.TblCanBo;
import com.lb.jwitter.service.TblCanBoService;

import com.lb.jwitter.service.TblDKMHDotDKHocVienChiTiet;
import com.lb.jwitter.service.TblDKMHDotDKHocVienChiTietService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblDKMHDotDKHocVienInsertAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private Long id; 
    public void setId(Long id) {
       this.id = id;
 }
    private String mssv;

    public String getMssv() {
		return mssv;
	}

	public void setMssv(String mssv) {
		this.mssv = mssv;
	}

	private int dotDKId; 
    public void setDotDKId(int dotDKId) {
       this.dotDKId = dotDKId;
 }

    public int getDotDKId() {
       return dotDKId;
 }

    private Collection<TblDKMHDotDK> tblDKMHDotDKs; 
    public Collection<TblDKMHDotDK>  getTblDKMHDotDKs(){ 
              return tblDKMHDotDKs; 
     } 

    private TblDKMHDotDKService tblDKMHDotDKService; 

    private int hocVienId; 
    public void setHocVienId(int hocVienId) {
       this.hocVienId = hocVienId;
 }

    public int getHocVienId() {
       return hocVienId;
 }

    private Collection<TblThongTinHocVien> tblThongTinHocViens; 
    public Collection<TblThongTinHocVien>  getTblThongTinHocViens(){ 
              return tblThongTinHocViens; 
     } 

    private TblThongTinHocVienService tblThongTinHocVienService; 

    private int soTCDangKyToiDa; 
    public void setSoTCDangKyToiDa(int soTCDangKyToiDa) {
       this.soTCDangKyToiDa = soTCDangKyToiDa;
 }

    public int getSoTCDangKyToiDa() {
       return soTCDangKyToiDa;
 }

    private int khongDangKyYN; 
    public void setKhongDangKyYN(int khongDangKyYN) {
       this.khongDangKyYN = khongDangKyYN;
 }

    public int getKhongDangKyYN() {
       return khongDangKyYN;
 }

    private String khongDangKyLyDo; 
    public void setKhongDangKyLyDo(String khongDangKyLyDo) {
       this.khongDangKyLyDo = khongDangKyLyDo;
 }

    public String getKhongDangKyLyDo() {
       return khongDangKyLyDo;
 }

    private int canBoId; 
    public void setCanBoId(int canBoId) {
       this.canBoId = canBoId;
 }

    public int getCanBoId() {
       return canBoId;
 }

    private Collection<TblCanBo> tblCanBos; 
    public Collection<TblCanBo>  getTblCanBos(){ 
              return tblCanBos; 
     } 

    private TblCanBoService tblCanBoService; 

    private String ngayCapNhat; 
    public void setNgayCapNhat(String ngayCapNhat) {
       this.ngayCapNhat = ngayCapNhat;
 }

    public String getNgayCapNhat() {
       return ngayCapNhat;
 }

    private TblDKMHDotDKHocVien tblDKMHDotDKHocVien; 
    public TblDKMHDotDKHocVien  getTblDKMHDotDKHocVien() {
        return tblDKMHDotDKHocVien;
 }

    private Collection<TblDKMHDotDKHocVien> tblDKMHDotDKHocViens; 
    public Collection<TblDKMHDotDKHocVien>  getTblDKMHDotDKHocViens() {
         return tblDKMHDotDKHocViens; 
      } 

    private TblDKMHDotDKHocVienService tblDKMHDotDKHocVienService; 

    private Collection<TblDKMHDotDKHocVienChiTiet> tblDKMHDotDKHocVienChiTiets; 
    public Collection<TblDKMHDotDKHocVienChiTiet>  getTblDKMHDotDKHocVienChiTiets() {
         return tblDKMHDotDKHocVienChiTiets; 
      } 

    private TblDKMHDotDKHocVienChiTietService tblDKMHDotDKHocVienChiTietService; 

	private int preInsert(Message msg, TblDKMHDotDKHocVien obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postInsert(Message msg, TblDKMHDotDKHocVien obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
    public String execute() throws Exception 
   { 
    try{ 
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_DKMH_DotDK_HocVien", "them")){ 
        return "noprivilege"; 
    }
        tblDKMHDotDKHocVienService = (TblDKMHDotDKHocVienService) context.getBean("tbl_DKMH_DotDK_HocVienClient"); 
        tblDKMHDotDKService = (TblDKMHDotDKService) context.getBean("tbl_DKMH_DotDKClient"); 

        tblThongTinHocVienService = (TblThongTinHocVienService) context.getBean("tbl_ThongTin_HocVienClient"); 

        tblCanBoService = (TblCanBoService) context.getBean("tbl_CanBoClient"); 

        tblDKMHDotDKHocVienChiTietService = (TblDKMHDotDKHocVienChiTietService) context.getBean("tbl_DKMH_DotDK_HocVien_ChiTietClient"); 

        msg = new Message(); 
        tblDKMHDotDKHocVien = new TblDKMHDotDKHocVien(); 
        
        tblDKMHDotDKHocVien.setDotDKId(tblDKMHDotDKService.gettblDKMHDotDK(new Long(dotDKId)));
        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
        if(mssv != null && !mssv.equals("") ){ 
            ConditionReport con = new ConditionReport(); 
            con.setCol("MSSV");
            con.setValue1(mssv);
            con.setOperator("eq");
            cons.add(con);	
        }
        List<TblThongTinHocVien> hocVien = tblThongTinHocVienService.findtblThongTinHocVienByCondition(cons, 1, 1);
        if(hocVien == null || hocVien.size() <1)
        	return Action.ERROR;
        
        tblDKMHDotDKHocVien.setHocVienId(hocVien.get(0)); 
        tblDKMHDotDKHocVien.setSoTCDangKyToiDa(soTCDangKyToiDa); 
        if(khongDangKyYN == 1){ 
           tblDKMHDotDKHocVien.setKhongDangKyYN(true); 
       } else { 
           tblDKMHDotDKHocVien.setKhongDangKyYN(false); 
       } 
        tblDKMHDotDKHocVien.setKhongDangKyLyDo(khongDangKyLyDo); 
        
        tblDKMHDotDKHocVien.setCanBoId(taiKhoanDangNhap.getCanBoId()); 
       DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
       Date dd = new Date(); 
       GregorianCalendar c = new GregorianCalendar(); 
       c.setTime(dd);
       XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblDKMHDotDKHocVien.setNgayCapNhat(d);
        
        preInsert(msg,tblDKMHDotDKHocVien); 
       if (msg.isPreMessage_b()) 
       { 
           id = tblDKMHDotDKHocVienService.createtblDKMHDotDKHocVien(tblDKMHDotDKHocVien); 
           postInsert(msg, tblDKMHDotDKHocVien); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
       tblDKMHDotDKHocVien.setId(id); 
       
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Insert tbl_DKMH_DotDK_HocVien Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 } 
 
